//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "client\fn_clientExecuteLead.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_destination_msg","_die_msg","_shared_destination","_shared_alive","_destination_code","_die_code","_destination","_action_display_timer","_leader","_idx"];
DEFAULT_PARAM(_object,TMF_EXECUTE_ACTION_OBJECT,objNull);
DEFAULT_PARAM(_caller,TMF_EXECUTE_ACTION_CALLER,objNull);
DEFAULT_PARAM(_act_id,TMF_EXECUTE_ACTION_ID,-1);
DEFAULT_PARAM(_arguments,TMF_EXECUTE_ACTION_ARGS,[]);
DEFAULT_PARAM(_action,TMF_EXECUTE_ACTION_ACTION,[]);

_action_id = _arguments select TMF_EXECUTE_ACTION_PARAM_ACTID;

if (!isnull _object) then {
	if (_action_id > 0) then {
		_instance_id = [_object] call FFUNC(getInstanceID);
		if (count _action  <= 0) then {
			_action = [_object,_action_id] call FFUNC(actionGet);
		};
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);
		
		_destination_msg = [_parameters, "DEST_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_die_msg = [_parameters, "DIE_MSG", "",THIS_FILE] call FFUNC(hashGet);
		_shared_destination = [_parameters, "SHARED_DEST_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_shared_alive = [_parameters, "SHARED_ALIVE_VAR", "",THIS_FILE] call FFUNC(hashGet);
		_destination = [_parameters, "DESTINATION", [],THIS_FILE] call FFUNC(hashGet);
		_destination_code = [_parameters, "DEST_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_die_code = [_parameters, "DIE_CODE", "",THIS_FILE] call FFUNC(hashGet);
		_shared_destination = _shared_destination call FFUNC(getStringVar);
		_shared_alive = _shared_alive call FFUNC(getStringVar);
		
		_obj_var_arrived = format["mission_person_arrived_%1",_action_id];
		_is_arrived = OGETVAR(_object,_obj_var_arrived,false);
		TRACE_2("Unit Var Get",_obj_var_arrived,_is_arrived);
		TRACE_1("Caller, Rescue,Leader",ARR_3(_caller,(ARR_2(_caller,"Rescue") call FFUNC(playerHasRole)), leader _caller));
		_object setCaptive false;
		If ([_caller,"Rescue"] call FFUNC(playerHasRole) && ((leader player) == (leader _caller))) then {
			if (!_is_arrived) then {
				if (player == (leader player)) then {
					_object enableAI "MOVE";
					[_object] joinSilent (group player);
					_object doFollow _caller;
					[_object,_caller] spawn {
						private ["_object","_caller"];
						PARAMS_2(_object,_caller);
						sleep 1;
						_object doFollow _caller;
						sleep 1;
						doStop _object;
						sleep 0.1;
						_object doFollow _caller;
						(group _caller) setBehaviour "SAFE";
						(group _caller) setSpeedMode "FULL";
						(group _caller) allowFleeing 0;
					};
					TRACE_1("Unit Joined Group",ARR_3(_caller,(group _caller),(group _object)));
					
					_idx = player addEventHandler ["killed",{
						private ["_player","_player_group","_ai","_group","_wp1","_wp2","_initial_side","_idx"];
						_idx = [_object,_action,"LEAD_EXECUTE_KILLED_EVT",-1] call FFUNC(actionGetLocal);
						if (_idx >= 0) then {
							player removeEventHandler ["killed", _idx];
						};
						_player = _this select 0;
						_player_group = group _player;
						TRACE_1("Entered Group Verification",ARR_3(_player, _player_group, units _player_group));
						{
							_ai = _x;
							if (!isPlayer _ai && {(OGETVAR(_ai,QGVAR(MISSION_INSTANCE_ID),"") != "")}) then {
								doStop _ai;
								_initial_side = OGETVAR(_ai, QGVAR(INITIAL_SIDE),GVAR(own_side));
								TRACE_1("Initial Side",_initial_side);
								[_ai] joinSilent ([_initial_side] call FFUNC(createGroup));
								doStop _ai;
								_group = group _ai;
								_group setBehaviour "STEALTH";
								_group setCombatMode "BLUE";
								_group allowFleeing 0;
								_group call FFUNC(deleteWaypoints);
								_wp1 = _group addWaypoint [getPos _ai, 0];
								_wp1 setWaypointType "MOVE";
								_wp2 = _group addWaypoint [getPos _ai, 0];
								_wp2 setWaypointType "CYCLE";
								_group setCurrentWaypoint _wp1;
								_ai doMove (getPos _ai);
								TRACE_1("AI Degrouped",ARR_3(_ai, group _ai, count (waypoints _group)));
							};
						} foreach units _player_group;
						TRACE_1("Exited Group Verification",ARR_3(_player, _player_group, units _player_group));
					}];
					[player,_action,"LEAD_EXECUTE_KILLED_EVT",_idx] call FFUNC(actionSetLocal);
				};
			};
		} else {
			[(["STR_TMF_LEAD_NOTRESCUE"] call FFUNC(translate))] call FFUNC(globalChat);
		};
	};
};

TRACE("End");
TMFPROFILERSTOP;